
usermessage.Hook( "PhaseCounter", function( phase_counter )
PhaseCounter = phase_counter:ReadLong() or 0; end )

usermessage.Hook( "Phase", function( new_phase )
Phase = new_phase:ReadLong() or 0; end )

usermessage.Hook( "PhaseLength", function( phase_length )
PhaseLength = phase_length:ReadLong() or 30; end )

--usermessage.Hook( "StartTime", function( start_time )
--StartTime = start_time:ReadFloat() or 0; end )

usermessage.Hook( "ServerSysTime", function( Sys_Time )
ServerSysTime = Sys_Time:ReadFloat() or 0; end )


local vars =
{
 
	font = "TargetID",
 
	padding = 4,
	margin = 35,
 
	text_spacing = 2,
	bar_spacing = 4,
 
	bar_height = 16,
 
	width = 0.25,
	
	SysTimeError = false
 
};

local colors =
{
 
	background =
	{
 
		border = Color( 220, 220, 220, 150 ),
		background = Color( 160, 160, 160, 75 )
 
	},
 
	text =
	{
 
		shadow = Color( 0, 0, 0, 200 ),
		text = Color( 255, 255, 255, 255 )
 
	},
 
	health_bar =
	{
 
		border = Color( 255, 0, 0, 255 ),
		background = Color( 255, 0, 0, 75 ),
		shade = Color( 255, 104, 104, 255 ),
		fill = Color( 232, 0, 0, 255 )
 
	},
        
        timer_bar =
	{
 
		border = Color( 0, 0, 255, 255 ),
		background = Color( 0, 0, 255, 75 ),
		shade = Color( 136, 136, 200, 255 ),
		fill = Color( 0, 0, 232, 255 )
 
	},
        
        phase_bar =
	{
 
		border = Color( 0, 200, 127, 255 ),
		background = Color( 0, 200, 127, 75 ),
		shade = Color( 127, 160, 80, 255 ),
		fill = Color( 0, 120, 60, 255 )
 
	}
 
};

local function HUDPaint( )
 
	client = client or LocalPlayer( );				-- set a shortcut to the client
	if( !client:Alive( ) ) then return; end				-- don't draw if the client is dead
 
	local _, th = good_hud:TextSize( "TEXT", vars.font );		-- get text size( height in this case )
 
	local i = 3;							-- shortcut to how many items( bars + text ) we have
 
	local width = vars.width * ScrW( );				-- calculate width
	local bar_width = width - ( vars.padding * i );			-- calculate bar width and element height
	local height = ( vars.padding * i ) + ( th * i ) + ( vars.text_spacing * i ) + ( vars.bar_height * i ) + vars.bar_spacing;
 
	local x = vars.margin;						-- get x position of element
	local y = ScrH( ) - vars.margin - height;			-- get y position of element
 
	local cx = x + vars.padding;					-- get x and y of contents
	local cy = y + vars.padding;
 
	good_hud:PaintPanel( x, y, width, height, colors.background );	-- paint the background panel
 
	local by = th + vars.text_spacing;				-- calc text position
 
	local text = string.format( "Health: %iHP", client:Health( ) );	-- get health text
	good_hud:PaintText( cx, cy, text, vars.font, colors.text );	-- paint health text and health bar
	good_hud:PaintBar( cx, cy + by, bar_width, vars.bar_height, colors.health_bar, client:Health( ) / 100 );
        
        local BestTime = tonumber(client:GetNetworkedFloat("BestTime", 0))
        local StartTime = tonumber(client:GetNetworkedFloat("StartTime",0))
        if !ServerSysTime then
		ServerSysTime = 0
		if !SysTimeError then
			print("SR: cl_hud ServerSysTime Error caught")
			vars.SysTimeError = true
		end
	elseif SysTimeError then
		print("SR: cl_hud ServerSysTime Error recovered")
	end
	local SplitTime = ServerSysTime - StartTime
        --if !BestTime then BestTime = 60 end
        --if !SplitTime then SplitTime = 30 end
        --if SplitTime > BestTime then SplitTime = BestTime; end
        
        by = by + vars.bar_height + vars.bar_spacing;			-- increment text position
        
	local text = string.format( "Best time: %s seconds", string.format('%1.3f',BestTime) );
        if client:Team() == TEAM_RACER then
            --text = text..string.format( ", Current time: %i seconds, Started: %f, Now: %f", SplitTime, StartTime, ServerSysTime );
            text = text..string.format( ", Current time: %s seconds", string.format('%1.3f',SplitTime) );
        end        
        
	good_hud:PaintText( cx, cy + by, text, vars.font, colors.text );	-- paint suit text and suit bar
	good_hud:PaintBar( cx, cy + by + th + vars.text_spacing, bar_width, vars.bar_height, colors.timer_bar, SplitTime / BestTime );
        
        by = by + th + vars.bar_height + vars.bar_spacing;
        
        local current_phase = "Unknown"
        
        if Phase == SR_WAITING then
            current_phase = "Next race starts"
        elseif Phase == SR_RACING then
            current_phase = "Race ends"
        end
        
        if !PhaseCounter then PhaseCounter = 17 end
        if !PhaseLength then PhaseLength = 60 end
        
        --local text = string.format( current_phase.." in %i seconds of %i", PhaseCounter,  PhaseLength);
        local text = string.format( current_phase.." in %i seconds", PhaseCounter);
	good_hud:PaintText( cx, cy + by, text, vars.font, colors.text );	-- paint suit text and suit bar
	good_hud:PaintBar( cx, cy + by + th + vars.text_spacing, bar_width, vars.bar_height, colors.phase_bar, PhaseCounter / PhaseLength );
 
end
hook.Add( "HUDPaint", "PaintOurHud", HUDPaint );

function hidehud(name) --hide original hud
	--for k, v in pairs{"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"} do
        for k, v in pairs{"CHudHealth", "CHudBattery"} do
		if name == v then return false end
	end
end
hook.Add("HUDShouldDraw", "hidehud", hidehud)
